package cn.walnut.web.async;

import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

/**
 * @program: walnut-security
 * @Date: 2018/12/21 22:09
 * @Author: Walnut1992
 * @Description:
 */
@RestController
public class AsyncController {

    @Autowired
    private MOckQueue mockQueue;

    @Autowired
    private DefferedResultHolder defferedResultHolder;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping("/order")
    public DeferredResult<String> order() {
        logger.info("主线程开始");
        String orderNumber = RandomStringUtils.randomNumeric(8);
        DeferredResult<String> result = new DeferredResult<>();
        defferedResultHolder.getMap().put(orderNumber, result);

//        Callable<String> result = new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                logger.info("副线程开始");
//                Thread.sleep(1000);
//                logger.info("副线程结束");
//                return "success";
//            }
//        };
        logger.info("主线程返回");
        return result;
    }
}
